﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using Microsoft.Xna.Framework;

namespace WindowsGame4
{
    abstract class Object
    {
        public enum TypeObject { player = 0, projectile = 1, enemy = 2, colision = 3, bomb = 4, explosion = 5, item = 6, wall = 7 };
        private String id;
        protected TypeObject type;
        protected Model model;
        protected String idAnimation;
        protected int numFrame;
        protected Position2D initialPosition;
        protected Position2D position;
        protected Position2D movement;
        protected Direction2D direction;
        private Boolean blockedMovement;
        protected int speed;
        String name;
        protected int kills;

        private int initialLives;
        private int initalLevel;
        private int lives;
        private Boolean visible;
        private ArrayList route;
        private Position2D previousPosition;
        private int level;
        private int gunAmmo;
        private int bombAmmo;
        private Boolean blinking;
        Boolean moving;
        int depth;
        Rectangle colision;
        Rectangle horizontalColision;
        Rectangle verticalColision;
        // Specifies when the object can attack (Depends of the level)
        private Boolean attack;
        int countBlinking;
        int reloadTime;
        String team;
        int height;
        Boolean fireFriendly;

        public String getStrType()
        {
            String result = "";
            switch (type)
            {
                case Object.TypeObject.player:
                    {
                        result = "Player";
                        break;
                    }
                case Object.TypeObject.enemy:
                    {
                        result = "Enemy";
                        break;
                    }

            }
            return result;
        }

        public Object(String id, Position2D position, Model model)
        {
            this.id = id;
            this.model = model;
            this.numFrame = 0;
            this.position = position;
            this.movement = null;
            blockedMovement = false;
            team = "";
            speed = 8;
            lives = 2;
            visible = true;
            attack = true;
            blinking = false;
            initialPosition = new Position2D(position.X,position.Y);
            countBlinking = 0;
            moving = false;
            reloadTime = 0;
            depth = 0;
            colision = new Rectangle(0,0,64,64);
            horizontalColision = new Rectangle(0, 0, 64, 64); ;
            verticalColision = new Rectangle(0, 0, 64, 64); ;
            kills = 0;
            fireFriendly = false;
        }

        public void updateGLX()
        {
            if (model != null)
            {
                numFrame++;
                if (numFrame >= model.getAnimation(idAnimation).getNumFrames())
                {
                    if (model.getAnimation(idAnimation).loop == false)
                        numFrame--;
                    else
                        numFrame = 0;
                }
            }
        }

        public void update()
        {
            /*
            if (movement != null)
            {
                int dx = position.X - movement.X;
                int dy = position.Y - movement.Y;

                if (dx == 0 && dy == 0)
                {
                    movement = null;

                    blockedMovement = false;
                    colision = false;
                }
                else
                {
                    if (dx != 0)
                    {
                        int copyPosition = position.X;
                        //position.X -= (speed * Math.Sign(dx) + speed * Math.Sign(dx));
                        // Check if is a colision
    
                            position.X = copyPosition - (speed * Math.Sign(dx));
                        //}
                    }
                    else
                    {
                        if (dy != 0)
                        {
                            int copyPosition = position.Y;
                            //position.Y -= (speed * Math.Sign(dy) + speed * Math.Sign(dy));
                            // Check if is a colision
      
                                position.Y = copyPosition - (speed * Math.Sign(dy));
                            //}
                        }
                    }
                }
            }
            */
            /*
            numFrame++;
            if (numFrame >= model.getAnimation(idAnimation).getNumFrames())
            {
                if (model.getAnimation(idAnimation).loop == false)
                    numFrame--;
                else
                    numFrame = 0;
            }*/
        }

        virtual public void draw()
        {
            
            int sizeX = model.getAnimation(idAnimation).getFrame(numFrame).texture.getWidth();
            int sizeY = model.getAnimation(idAnimation).getFrame(numFrame).texture.getHeight();
            int marginX = (GameInfo.getInstance().World.Size - sizeX) / 2;
            int marginY = (GameInfo.getInstance().World.Size - sizeY) / 2;
            //model.getAnimation(idAnimation).getFrame(numFrame).draw(tile.getRealX() + margin, tile.getRealY() + margin);
            if (blinking == false)
            {
               if (sizeY > 64)
                        model.getAnimation(idAnimation).getFrame(numFrame).draw(position.X + marginX, (position.Y - 20) + marginY);
               else
                model.getAnimation(idAnimation).getFrame(numFrame).draw(position.X + marginX, position.Y + marginY);

            }
            else
            {
                if (countBlinking == 1)
                {
                    if (sizeY > 64)
                        model.getAnimation(idAnimation).getFrame(numFrame).draw(position.X + marginX, (position.Y - 20) + marginY);
                    else
                        model.getAnimation(idAnimation).getFrame(numFrame).draw(position.X + marginX, position.Y + marginY);
                    countBlinking = 0;
                }
                else
                    countBlinking++;
            }

            if (type == TypeObject.player || type == TypeObject.enemy)
            {
                double max = 80;
                double width = ((100.0 * (double)reloadTime) / max);
                if (width > 0)
                    width = 1 - (width / 100.0);
                else
                    width = 1;

                Game1.spriteBatch.Begin();
                Game1.spriteBatch.Draw(Game1.textEnergyBorder, new Rectangle(position.X, position.Y - 55, (int)(Game1.textEnergyBorder.Width), Game1.textEnergyBorder.Height), Color.White);
                Game1.spriteBatch.Draw(Game1.textEnergy, new Rectangle(position.X, position.Y - 55, (int)(Game1.textEnergy.Width * width), Game1.textEnergy.Height), Color.White);

                max = initialLives;
                width = ((100.0 * (double)lives) / max);
                if (width > 0)
                    width = (width / 100.0);
                else
                    width = 1;
                Game1.spriteBatch.Draw(Game1.textLifeBorder, new Rectangle(position.X, position.Y - 60, (int)(Game1.textLifeBorder.Width), Game1.textLifeBorder.Height), Color.White);
                Game1.spriteBatch.Draw(Game1.textLife, new Rectangle(position.X, position.Y - 60, (int)(Game1.textLife.Width * width), Game1.textLife.Height), Color.White);

                int posX = position.X;
                for (int i = 0; i < level; i++)
                {
                    Game1.spriteBatch.Draw(Game1.textMiniStar, new Rectangle(posX, position.Y - 70, (int)(Game1.textMiniStar.Width), Game1.textMiniStar.Height), Color.White);
                    posX += Game1.textMiniStar.Width;
                }

                //Game1.spriteBatch.Draw(Game1.textEnergy, new Rectangle(position.X, position.Y - 55, (int)(Game1.textEnergy.Width * width), Game1.textEnergy.Height), Color.White);
                if(team == "red")
                    Game1.spriteBatch.DrawString(Game1.font, name, new Vector2(position.X, position.Y - 100), Color.Red);
                if(team == "green")
                    Game1.spriteBatch.DrawString(Game1.font, name, new Vector2(position.X, position.Y - 100), Color.Green);
                Game1.spriteBatch.End();
            }
        }

        public void changeAnimation(String idAnimation)
        {
            if (this.idAnimation != idAnimation)
            {
                this.idAnimation = idAnimation;
                this.numFrame = 0;
            }
        }

        public Model Model
        {
            get { return model; }
            set { model = value; }
        }

        public Position2D Position
        {
            get { return position; }
            set { position = value; }
        }
        public Position2D InitialPosition
        {
            get { return initialPosition; }
            set { initialPosition = value; }
        }
        

        public Direction2D Direction
        {
            get { return direction; }
            set
            {
                direction = value;

                if (type == Object.TypeObject.enemy)
                {
                    /*
                    switch (direction.getDirection())
                    {
                        case "left":
                            {
                                changeAnimation("left");
                                break;
                            }
                        case "right":
                            {
                                changeAnimation("right");
                                break;
                            }
                        case "up":
                            {
                                changeAnimation("back");
                                break;
                            }
                        case "down":
                            {
                                changeAnimation("front");
                                break;
                            }
                    }*/
                    changeAnimation("move_" + Direction.getDirection());
                }
            }
        }


        public Position2D Movement
        {
            get { return movement; }
            set { movement = value; }
        }

        public String Id
        {
            get { return id; }
            set { id = value; }
        }

        public TypeObject Type
        {
            get { return type; }
            set { type = value; }
        }
        public String Team
        {
            get { return team; }
            set { team = value; }
        }

        public String IdAnimation
        {
            get { return idAnimation; }
            set { idAnimation = value; }
        }
        public String Name
        {
            get { return name; }
            set { name = value; }
        }
        
        public Boolean BloquedMovement
        {
            get { return blockedMovement; }
            set { blockedMovement = value; }
        }
        public Boolean Visible
        {
            get { return visible; }
            set { visible = value; }
        }

        public int Speed
        {
            get { return speed; }
            set { speed = value; }
        }
        public Position2D PreviousPosition
        {
            get { return previousPosition; }
            set { previousPosition = value; }
        }
        public int Lives
        {
            get { return lives; }
            set { lives = value; }
        }
        public int InitialLives
        {
            get { return initialLives; }
            set { initialLives = value; }
        }
        public int InitalLevel
        {
            get { return initalLevel; }
            set { initalLevel = value; }
        }    
        
        public ArrayList Route
        {
            get { return route; }
            set { route = value; }
        }

        public int Level
        {
            get { return level; }
            set { level = value; }
        }

        public int BombAmmo
        {
            get { return bombAmmo; }
            set { bombAmmo = value; }
        }

        public int GunAmmo
        {
            get { return gunAmmo; }
            set { gunAmmo = value; }
        }

        public int Kills
        {
            get { return kills; }
            set { kills = value; }
        }
        
        public Boolean Attack
        {
            get { return attack; }
            set { attack = value; }
        }

        public Boolean Blinking
        {
            get { return blinking; }
            set { blinking = value; }
        }
        public Boolean FireFriendly
        {
            get { return fireFriendly; }
            set { fireFriendly = value; }
        }
        
        public Boolean Moving
        {
            get { return moving; }
            set { moving = value; }
        }     
        public int ReloadTime
        {
            get { return reloadTime; }
            set { reloadTime = value; }
        }
        public int Height
        {
            get { return height; }
            set { height = value; }
        }
        public int Depth
        {
            get { return depth; }
            set { depth = value; }
        }
        public Rectangle Colision
        {
            get { return colision; }
            set { colision = value; }
        }    
        public Rectangle VerticalColision
        {
            get { return verticalColision; }
            set { verticalColision = value; }
        }   
        public Rectangle HorizontalColision
        {
            get { return horizontalColision; }
            set { horizontalColision = value; }
        }   


    }
}
